﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;


namespace RemoteUser
{
    public partial class PLCAllParmsForm : Form
    {
        #region  内部变量
        private NetworkStream myStream;
        private Socket mySocket;
        private string myID;
        private string PLCID;
        //保存的PLC列表窗口对象，以便调用其公共成员或方法
        private PLCListForm myFatherForm;
        //周期性发送参数请求的线程
        private Thread allParmsThread;
        public delegate void ModifyTheLabelOfAllParmsForm(string newLabel);
        public delegate void ModifyPLCParmsList(object theParms1,object theParms2);
        #endregion
        
        public PLCAllParmsForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 重载的构造函数，修改了关闭事件和PLC标签
        /// </summary>
        /// <param name="netstream"></param>
        /// <param name="clientSocket"></param>
        /// <param name="ID"></param>
        /// <param name="selectedPLCID"></param>
        /// <param name="selectedPLCName"></param>
        /// <param name="PLCListViewForm"></param>
        public PLCAllParmsForm(NetworkStream netstream, 
                                Socket clientSocket,
                                string ID, 
                                string selectedPLCID,
                                string selectedPLCName,
                                PLCListForm PLCListViewForm)
        {
            InitializeComponent();
            myStream = netstream;
            mySocket = clientSocket;
            myID = ID;
            PLCID = selectedPLCID;
            myFatherForm = PLCListViewForm;
            this.PLCNameLabel.Text ="PLC名称： " +selectedPLCName;
            this.currentPLCIDLabel.Text ="PLCID:    " +selectedPLCID;
            this.Closing += new CancelEventHandler(PLCAllParameterForm_FormClosing);
        }

        private void PLCAllParmsForm_Load(object sender, EventArgs e)
        {
            allParmsThread = new Thread(AllParmsRequest);
            allParmsThread.IsBackground = true;
            allParmsThread.Start();
        }
        /// <summary>
        /// 关闭处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PLCAllParameterForm_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
                myFatherForm.PLCAllParmsFormIsExisted = false;
                allParmsThread.Abort();
                e.Cancel = false;
        }
     
        public void ModifyMyParmsData(object theParms1,object theParms2)
        {
                produceCraftsDataGridView.DataSource = (BindingCollection<ParmNode>)theParms1;
                deviceStatusDataGridView.DataSource = (BindingCollection<ParmNode>)theParms2;                
                produceCraftsDataGridView.Refresh();
                deviceStatusDataGridView.Refresh();          
        }
    
        /// <summary>
        /// 主动请求刷新参数表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void refreshAllParmsButton_Click(object sender, EventArgs e)
        {
            SendAndRecv.SendPLCAllParmsRequest(myStream,myID,PLCID);
        }
        /// <summary>
        /// 自动请求刷新参数表的线程函数
        /// </summary>
        private void AllParmsRequest()
        {
            try
            {
                while (true)
                {
                    SendAndRecv.SendPLCAllParmsRequest(myStream, myID, PLCID);
                    Thread.Sleep(10000);
                }
            }
            catch(ThreadAbortException ex)
            {
                Debug.WriteLine(ex.Message);
                Thread.ResetAbort();
            }
        }

        private void ProduceCraftsParmDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            produceCraftsDataGridView.Columns["parmName"].HeaderText = "参数";
            produceCraftsDataGridView.Columns["parmValue"].HeaderText = "数值";
        }

        private void DeviceStatusParmDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            deviceStatusDataGridView.Columns["parmName"].HeaderText = "参数";
            deviceStatusDataGridView.Columns["parmValue"].HeaderText = "数值";
        }
    }
}
